const fs = require('fs')
const path = require('path')
/**
 * 更新版本号
 * 1. 读取并更新 VersionCheck.vue 中的版本号
 * 2. 同步更新 versionCheck.json 中的版本号
 * 3. 自动将补丁版本号(第三位)加1
 */
const updateComponentVersion = () => {
	const versionFilePath = path.join(__dirname, '../src/components/VersionCheck/VersionCheck.vue')
	let content = fs.readFileSync(versionFilePath, 'utf-8')
	const versionRegex = /CURRENT_VERSION = '(\d+\.\d+\.\d+)'/
	const match = content.match(versionRegex)

	if (match) {
		const currentVersion = match[1]
		const versions = currentVersion.split('.')
		versions[2] = parseInt(versions[2]) + 1
		const newVersion = versions.join('.')

		content = content.replace(versionRegex, `CURRENT_VERSION = '${newVersion}'`)
		fs.writeFileSync(versionFilePath, content)

		// 同时更新 versionCheck.json
		const jsonPath = path.join(__dirname, '../public/versionCheck.json')
		const jsonContent = require(jsonPath)
		jsonContent.version = newVersion
		fs.writeFileSync(jsonPath, JSON.stringify(jsonContent, null, 4))

		console.log(`版本号已更新: ${currentVersion} -> ${newVersion}`)
	}
}

updateComponentVersion()
